package server

import (
	"context"
	"fmt"
	"srv-server/basic/global"
	__ "srv-server/basic/proto"
	"srv-server/handler/dao"
	"srv-server/handler/model"
	"srv-server/pkg"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedSrvServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {
	var user model.User

	global.DB.Where("mobile = ?", in.Mobile).Find(&user)
	if user.Id == 0 {
		return &__.LoginResponse{
			Code: 500,
			Msg:  "账号不存在",
		}, nil
	}
	fmt.Println(user.Password)
	if in.Password != user.Password {
		return &__.LoginResponse{
			Code: 500,
			Msg:  "密码错误",
		}, nil
	}
	if !pkg.RealName(in.Name, in.IdNumber) {
		return &__.LoginResponse{
			Code: 500,
			Msg:  "实名认证失败",
		}, nil
	}
	return &__.LoginResponse{
		Code: 200,
		Msg:  "登录成功",
	}, nil
}
func (s *Server) BuyTicket(_ context.Context, in *__.BuyTicketRequest) (*__.BuyTicketResponse, error) {
	var user model.User
	global.DB.Where("id = ?", in.BuyUserId).Find(&user)
	if user.Id == 0 {
		return &__.BuyTicketResponse{
			Code: 500,
			Msg:  "账户信息错误",
		}, nil
	}
	var buy model.BuyTicket
	global.DB.Where("id = ?", in.TicketId).Find(&buy)
	if buy.Id == 0 {
		return &__.BuyTicketResponse{
			Code: 500,
			Msg:  "当前票不存在",
		}, nil
	}
	buy = model.BuyTicket{

		BuyUserId: uint(in.BuyUserId),
		TicketId:  uint(in.TicketId),
	}
	var tick model.Ticket
	global.DB.Where("id = ?", in.TicketId).Find(&tick)
	if tick.VoteType == 3 {
		return &__.BuyTicketResponse{
			Code: 500,
			Msg:  "购票失败，票已售罄",
		}, nil
	}
	if err := global.DB.Create(&buy).Error; err != nil {
		return &__.BuyTicketResponse{
			Code: 500,
			Msg:  "购票失败",
		}, nil
	}
	return &__.BuyTicketResponse{
		Code: 200,
		Msg:  "购票成功",
	}, nil
}

func (s *Server) FindRemaTicket(_ context.Context, in *__.FindRemaTicketRequest) (*__.FindRemaTicketResponse, error) {
	var tick model.Ticket
	global.DB.Where("id = ?", in.TicketId).Find(&tick)
	if tick.Id == 0 {
		return &__.FindRemaTicketResponse{}, nil
	}
	return &__.FindRemaTicketResponse{
		TicketName:       tick.TicketName,
		RemainingTickets: int32(tick.RemainingTickets),
		VoteType:         int32(tick.VoteType),
	}, nil

}
func (s *Server) Findticket_name(_ context.Context, in *__.FindTicketNameRequest) (*__.FindTicketNameResponse, error) {
	var tick model.Ticket
	global.DB.Where("ticket_name = ?", in.TicketName).Find(&tick)

	return &__.FindTicketNameResponse{
		TicketName: tick.TicketName,
		Price:      float32(tick.Price),
		Seat:       tick.Seat,
	}, nil

}
func (s *Server) ListTicketType(_ context.Context, in *__.ListTicketTypeRequest) (*__.ListTicketTypeResponse, error) {
	var tick model.Ticket
	listTick, _ := dao.GetListTick(int(in.TicketType), tick)
	var am []*__.GetTick
	for _, ticket := range listTick {
		getTick := __.GetTick{
			TicketName:       ticket.TicketName,
			RemainingTickets: int32(ticket.RemainingTickets),
			VoteType:         int32(ticket.VoteType),
			Price:            float32(ticket.Price),
			Seat:             ticket.Seat,
			SeatType:         int32(ticket.SeatType),
			TicketType:       int32(ticket.TicketType),
		}
		am = append(am, &getTick)
	}
	return &__.ListTicketTypeResponse{
		List: am,
	}, nil

}
